#include <Lithne.h>

#define FUNCTION_DB 200

// only used for example purposes
long timer = millis();
int cnt = 0;
  
void setup() 
{
  Lithne.init(115200);
  Lithne.addNode(0, XBeeAddress64(0x00000000, 0x00000000)); // Node 0 is always the PAN coordinator
  Lithne.addNode(11, XBeeAddress64(0x0013a200, 0x4071cdff)); // Add as many nodes as you like
  Lithne.addNode(13, XBeeAddress64(0x0013a200, 0x4071cdfe)); // Add as many nodes as you like
  Lithne.addNode(16, XBeeAddress64(0x0013a200, 0x4071cdfb)); // Add as many nodes as you like
  Lithne.addNode(255, XBeeAddress64(0x00000000, 0x0000ffff)); // This is the address for a broadcast message
}


void loop() 
{
  if (Lithne.available()) 
  {
    functionTable(Lithne.getFunction(), Lithne.getNumberOfArguments(), Lithne.getSender());
  }
  
  if (Lithne.newDBMeasurement(0)) 
  { // We have received a DB signal strength for node 0; If you would like to check other nodes, you will have to separately check them
    int dbStrength = Lithne.getDB(0); // actually get the signal strength from node 0
    
    // send the signal strength back to the coordinator, but ofcourse you can do something else with it
    Lithne.addArgument(dbStrength);
    Lithne.send(0, FUNCTION_DB); // Send to Node 0 Function 254
  }
  
  timedDBRequest(); // send a DB request every so many seconds
}

void timedDBRequest() {
  if (millis() - timer > 5000) {
    timer = millis();
    cnt++;
    Lithne.sendDBRequest(XBeeAddress64(0x00000000, 0x00000000));
  }
}
